package com.sjgl.esdn.controller;

import com.sjgl.esdn.domain.AjaxResultESDN;
import com.sjgl.esdn.domain.entity.FileBrowse;
import com.sjgl.esdn.domain.entity.FileDownload;
import com.sjgl.esdn.domain.entity.FileManagement;
import com.sjgl.esdn.domain.entity.vo.*;
import com.sjgl.esdn.service.FileManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/fileLog")
public class FileManageController {
    @Autowired
    private FileManageService fileManageService;

    @RequestMapping("/fileList")
    private AjaxResultESDN fileList(){
        List<FileManagement> list = fileManageService.showAll();
        return AjaxResultESDN.success(list);
    }

    @GetMapping("/fileBrowseList")
    private AjaxResultESDN fileBrowseList(Long fileId){
        List<FileBrowse> list = fileManageService.showAllBrowse(fileId);
        return AjaxResultESDN.success(list);
    }

    @RequestMapping("/fileDownloadList")
    private AjaxResultESDN fileDownloadList(Long fileId){
        List<FileDownload> list = fileManageService.showAllDownload(fileId);
        return AjaxResultESDN.success(list);
    }

    @RequestMapping("/fileSearch")
    private AjaxResultESDN fileSearch(String keyword){
        List<FileManagement> list = fileManageService.fileSearch("%"+keyword+"%");
        return AjaxResultESDN.success(list);
    }

    @RequestMapping("/fileBrowseSearch")
    private AjaxResultESDN browseSearch(String keyword, Long fileId){
        List<FileBrowse> list = fileManageService.fileBrowse("%"+keyword+"%",fileId);
        return AjaxResultESDN.success(list);
    }

    @RequestMapping("/fileDownloadSearch")
    private AjaxResultESDN downloadSearch(String keyword, Long fileId){
        List<FileDownload> list = fileManageService.fileDownload("%"+keyword+"%",fileId);
        return AjaxResultESDN.success(list);
    }

    @RequestMapping("/fileOff")
    private AjaxResultESDN updateType(@RequestBody FileOffVo fileOffVo) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date abandonTime = sdf.parse(fileOffVo.getAbandonTime());
        int result = fileManageService.updateFileType(fileOffVo.getFileId(), fileOffVo.getAbandonReason(),abandonTime, fileOffVo.getAbandonId());
        return AjaxResultESDN.success(result);
    }

}
